#ifndef H_RCGD_RTC_H
#define H_RCGD_RTC_H   

/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*
#define RTC_ADDR_0			((unsigned short *)(RTC_BASE_ADDR+ (0x00<<1)  ))
#define RTC_ADDR_1			((unsigned short *)(RTC_BASE_ADDR+ (0x01<<1)  ))
#define RTC_ADDR_2			((unsigned short *)(RTC_BASE_ADDR+ (0x02<<1)  ))
#define RTC_ADDR_3			((unsigned short *)(RTC_BASE_ADDR+ (0x03<<1)  ))
#define RTC_ADDR_4			((unsigned short *)(RTC_BASE_ADDR+ (0x04<<1)  ))
#define RTC_ADDR_5			((unsigned short *)(RTC_BASE_ADDR+ (0x05<<1)  ))
#define RTC_ADDR_6			((unsigned short *)(RTC_BASE_ADDR+ (0x06<<1)  ))
#define RTC_ADDR_7			((unsigned short *)(RTC_BASE_ADDR+ (0x07<<1)  ))
#define RTC_ADDR_8			((unsigned short *)(RTC_BASE_ADDR+ (0x08<<1)  ))
#define RTC_ADDR_9			((unsigned short *)(RTC_BASE_ADDR+ (0x09<<1)  ))
#define RTC_ADDR_A			((unsigned short *)(RTC_BASE_ADDR+ (0x0A<<1)  ))
#define RTC_ADDR_B			((unsigned short *)(RTC_BASE_ADDR+ (0x0B<<1)  ))
#define RTC_ADDR_C			((unsigned short *)(RTC_BASE_ADDR+ (0x0C<<1)  ))
#define RTC_ADDR_D			((unsigned short *)(RTC_BASE_ADDR+ (0x0D<<1)  ))
#define RTC_ADDR_E			((unsigned short *)(RTC_BASE_ADDR+ (0x0E<<1)  ))

#define RTC_SECONDS      	(*RTC_ADDR_0 &0xFF)
#define RTC_SECONDS_ALARM	(*RTC_ADDR_1 &0xFF)
#define RTC_MINUTES			(*RTC_ADDR_2 &0xFF)
#define RTC_MINUTES_ALARM	(*RTC_ADDR_3 &0xFF)
#define RTC_HOURS			(*RTC_ADDR_4 &0xFF)
#define RTC_HOURS_ALARM		(*RTC_ADDR_5 &0xFF)
#define RTC_DAY_OF_WEEK		(*RTC_ADDR_6 &0xFF)
#define RTC_DAY_OF_MONTH	(*RTC_ADDR_7 &0xFF)
#define RTC_MONTH			(*RTC_ADDR_8 &0xFF)
#define RTC_YEAR			(*RTC_ADDR_9 &0xFF)
#define RTC_REG_A			(*RTC_ADDR_A &0xFF)
#define RTC_REG_B			(*RTC_ADDR_B &0xFF)
#define RTC_REG_C			(*RTC_ADDR_C &0xFF)
#define RTC_REG_D			(*RTC_ADDR_D &0xFF)
#define RTC_CENTURY			(*RTC_ADDR_E &0xFF)
*/

typedef struct _RTC_STRUCT{
	unsigned short	Century;
	unsigned short  Year;
	unsigned short	Month;

	unsigned short  Day;
	unsigned short  Hour;
	unsigned short  Min;
	unsigned short  Sec;

} RTC_STRUCT ;


//#define	GetFPGAValue(Addr)	( (*Addr) & 0x00FF )


#define GetRTCValue()	GetFPGAValue(RTC_DATA_Addr)
#define SetRTCValue(x)	SetFPGAValue(RTC_DATA_Addr, x)   //kang *RTC_DATA_Addr=(x)

#define GetRTCValueINTEL()	GetFPGAValue(RTC_DATA_INTEL_Addr)

unsigned short RTCRead(unsigned short Addr);
void RTCWrite(unsigned short Addr,unsigned short x);

#define	ToBCD(x)	( ( (x/10)*16) + (x%10) )
#define ToBINARY(x) (   (x/16)*10  + (x%16)  )


#define RTC_ADDR_0			0
#define RTC_ADDR_1			1
#define RTC_ADDR_2			2
#define RTC_ADDR_3			3
#define RTC_ADDR_4			4
#define RTC_ADDR_5			5
#define RTC_ADDR_6			6
#define RTC_ADDR_7			7
#define RTC_ADDR_8			8
#define RTC_ADDR_9			9
#define RTC_ADDR_A			10
#define RTC_ADDR_B			11
#define RTC_ADDR_C			12
#define RTC_ADDR_D			13
#define RTC_ADDR_E			14
#define RTC_ADDR_F			15

#define RTC_SECONDS      	RTC_ADDR_0
#define RTC_SECONDS_ALARM	RTC_ADDR_1
#define RTC_MINUTES			RTC_ADDR_2
#define RTC_MINUTES_ALARM	RTC_ADDR_3

#define RTC_HOURS			RTC_ADDR_4
#define RTC_HOURS_ALARM		RTC_ADDR_5
#define RTC_DAY_OF_WEEK		RTC_ADDR_6
#define RTC_DAY_OF_MONTH	RTC_ADDR_7

#define RTC_MONTH			RTC_ADDR_8
#define RTC_YEAR			RTC_ADDR_9
#define RTC_REG_A			RTC_ADDR_A
#define RTC_REG_B			RTC_ADDR_B

#define RTC_REG_C			RTC_ADDR_C
#define RTC_REG_D			RTC_ADDR_D
#define RTC_CENTURY			RTC_ADDR_E

#define M_VALUE_START_RTC	0x20
#define M_VALUE_STOP_RTC	0x60



#define GetRtcBinary(x)		ToBINARY( RTCReadINTEL(x) )
/*RTC�BCD���� ; Binary�ٲ����*/

#define GetRtcSeconds()	GetRtcBinary(RTC_SECONDS)
#define GetRtcMinutes()	GetRtcBinary(RTC_MINUTES)
#define GetRtcHours()	GetRtcBinary(RTC_HOURS)
#define GetRtcDay()		GetRtcBinary(RTC_DAY_OF_MONTH)
#define GetRtcMonth()	GetRtcBinary(RTC_MONTH)
#define GetRtcYear()	GetRtcBinary(RTC_YEAR)
#define GetRtcCentury()	GetRtcBinary(RTC_CENTURY)

#define SetRtcBCD(Addr,x)	RTCWriteINTEL(Addr,ToBCD(x) );

#define SetRtcSeconds(x)	SetRtcBCD(RTC_SECONDS,x)
#define SetRtcMinutes(x)	SetRtcBCD(RTC_MINUTES,x)
#define SetRtcHours(x)		SetRtcBCD(RTC_HOURS,x)
#define SetRtcDay(x)		SetRtcBCD(RTC_DAY_OF_MONTH,x)
#define SetRtcMonth(x)		SetRtcBCD(RTC_MONTH,x)
#define SetRtcYear(x)		SetRtcBCD(RTC_YEAR,x)
#define SetRtcCentury(x)	SetRtcBCD(RTC_CENTURY,x)


/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/
/*RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-RTC Define-*/




typedef struct{
	time_	stime;
	time_	etime;
	int		check_counter;
	int		stop_counter;
	int		malfunc_counter;
	char	start;
} rtc_;

extern rtc_	rtc;

extern char check_rtc();
extern void start_rtc();
extern void Init_RTC();
extern void maintain_rtc();

#endif
